#pragma once
#ifndef xMisc_h__
#define xMisc_h__

#include "../../../dxhead/dxegheader.h"
#include "../../../xlib/xlib.h"

namespace Sky
{
typedef IDirect3D9* (WINAPI * LPDIRECT3DCREATE9) (UINT);
class xMisc
{
private:
		static HMODULE s_hModD3D9;
		static TChar s_strMediaSearchPath[MAX_PATH];
		static LPDIRECT3DCREATE9 s_DynamicDirect3DCreate9;
		static TChar* GetMediaSearchPath();
		static TBool EnsureD3DAPIs();
		static HRESULT SetMediaSearchPath( TChar* strPath );
public:
		DXAPI static IDirect3D9 * WINAPI Dynamic_Direct3DCreate9(UINT SDKVersion);
		DXAPI static HRESULT FindDXSDKMediaFileCch( TChar* strDestPath, TInt cchDest, TChar* strFilename );
		DXAPI static TBool	FindMediaSearchParentDirs( TChar* strSearchPath, TInt cchSearch, TChar* strStartAt, TChar* strLeafName );
		DXAPI static TBool	FindMediaSearchTypicalDirs( TChar* strSearchPath, TInt cchSearch, TChar* strLeaf, TChar* strExePath, TChar* strExeName );
		DXAPI static TInt	GethashCode(const TChar* szStr)
		{
			TUint hash = 0;
			TChar *p; 
			if (szStr == vNull)
				return 0;

			for(p = (TChar*) szStr; *p; ++p)
			{
				hash = (hash >> 13 ) | (hash << 19);
				hash += *p;
			}
			return hash ;
		}
		DXAPI static inline VOID SetRect(LPRECT r,TInt xLeft,TInt yTop,TInt xRight,TInt yBotton)
		{
			r->left=xLeft;
			r->top=yTop;
			r->bottom=yBotton;
			r->right=xRight;
		}

		DXAPI static TBool InRect(LPRECT pr,TInt xPos,TInt yPos)
		{
			if(xPos>pr->left&&xPos<pr->right)
				if(yPos>pr->top&&yPos<pr->bottom)
					return vTrue;
			return vFalse;
		}

		//static void* ID2Ptr(TUint id)
		//{
		//	return (void*)id;
		//}
		DXAPI static void Pick(TInt client_x,TInt client_y,
			TInt back_buffer_width,TInt back_buffer_height,
			const D3DXMATRIX* pmatWorld,
			const D3DXMATRIX* pmatView,
			const D3DXMATRIX* pmatProj, 
			D3DXVECTOR3* pPickRayDir,
			D3DXVECTOR3* pRayOrig );
};
}
#endif